<?php
 ## 18/09/2003

## v1.1 Stable ##
## Changelog:
##   -Bug fix: string length cannot be overwritten to smaller than default
######
## Verification Image
## pt_br: Imagem de Verifica�o
######
## This class generate an image with random text
## to be used in form verification. It has visual
## elements design to confuse OCR software preventing
## the use of BOTS.
##
## pt_br:
## Esta classe  gera uma imagem com texto randomico
## para ser usada em valida�o de formularios. Ela tem
## elementos visuais desenhados para confundir softwares
## de OCR, prevenindo o uso de BOTS.
##
#######
## Author: Rafael Machado Dohms (DoomsDay)
## Email: dooms@terra.com.br
##
## 18/09/2003
#######
## Usage: See attached files
## Uso: Ver anexos
## 		OR / OU
## http://planeta.terra.com.br/informatica/d2000/vImage/vImage_withexamples.zip
#####
## Modified by: Meth
## Contact: mth at mthweb.org
## Deleted some functions and added class funciones.class.php to generate a better random string
#####
require_once(dirname(__FILE__).'/funciones.class.php');

class vImage{

	var $numChars = 3; # Tamanho da String: default 3;
	var $w; # Largura da imagem
	var $h = 20; # Altura da Imagem: default 15;
	var $colBG = "240 240 240";
	var $colTxt = "0 0 0";
	var $colBorder = "255 255 255";
	var $charx = 15; # Espa lateral de cada char
	var $numCirculos = 10; #Numeros de circulos randomicos
    var $font_file = "clases/comic.ttf";
    var $funciones;


	function vImage(){
		$this->funciones = new funciones;
	}

	function gerText($num=5){
		$this->numChars = $num;
		$this->texto = $this->funciones->random_string($num,'abcdefghijkmnpqrstuvwxyz0123456789');
		return $this->texto;
	}

	function showImage(){
        $this->gerImage();
		header("Content-type: image/png");
		ImagePng($this->im);
	}

	function gerImage(){
		# Calcular tamanho para caber texto
		$this->w = ($this->numChars*$this->charx) + 40; #5px de cada lado, 4px por char
		# Criar img
		$this->im = imagecreatetruecolor($this->w, $this->h);
		#desenhar borda e fundo
		imagefill($this->im, 0, 0, $this->getColor($this->colBorder));
		imagefilledrectangle ( $this->im, 1, 1, ($this->w-2), ($this->h-2), $this->getColor($this->colBG) );

        if ($this->circulos){
    		#desenhar circulos
    		for ($i=1;$i<=$this->numCirculos;$i++) {
    			$randomcolor = imagecolorallocate ($this->im , rand(100,255), rand(100,255),rand(100,255));
    			imageellipse($this->im,rand(0,$this->w-10),rand(0,$this->h-3), rand(20,60),rand(20,60),$randomcolor);
    		}
        }
        else{
            for ($i=0;$i<10;$i++) {
              $size=intval(rand(6,14));
              $angle=intval(rand(0,360));
              $x=intval(rand(10,$this->w-10));
              $y=intval(rand(0,$this->h-5));
              $color=imagecolorallocate($this->im,intval(rand(160,224)),intval(rand(160,224)),intval(rand(160,224)));
              $text=chr(intval(rand(45,250)));
              ImageTTFText ($this->im,$size,$angle,$x,$y,$color,$this->font_file,$this->texto);
            }
        }

      for ($i=0,$x=10; $i<$this->numChars;$i++) {
        $r=intval(rand(0,128));
        $g=intval(rand(0,128));
        $b=intval(rand(0,128));
    	$char = strtoupper(substr($this->texto, $i, 1));
        $color = ImageColorAllocate($this->im, $r,$g,$b);
        $shadow= ImageColorAllocate($this->im, $r+128, $g+128, $b+128);
        $size=intval(rand(10,14));
        $angle=intval(rand(-30,30));
        ImageTTFText($this->im,$size,$angle,$x+4,16,$shadow,$this->font_file,$char);
        ImageTTFText($this->im,$size,$angle,$x,16,$color,$this->font_file,$char);
        $x+=$size+8;
      }

	}

	function getColor($var){
		$rgb = explode(" ",$var);
		$col = imagecolorallocate ($this->im, $rgb[0], $rgb[1], $rgb[2]);
		return $col;
	}
}

#dooms@terra.com.br# ?>
